<template>
    <div class="add-test">
        <Card shadow>
             <Form :model="formLeft"  :label-width="100" :rules="ruleValidate" ref="formLeft">
             	<Row>
             		<Col :sm="20" :md="16" :lg="14">
				        <FormItem label="试卷名称：" prop="name">
				            <Input v-model="formLeft.name"></Input>
				        </FormItem>
			        </Col>
		        </Row>
		        <Row :gutter="10">
             		<Col :sm="4" :md="6" :lg="8">
				        <FormItem label="考试时长：" prop="duration">
			            	<Input v-model="formLeft.duration"></Input>
				        </FormItem>
		         	</Col>
		         	<Col :sm="2" :md="2" :lg="2" class="unit">
				     	  	分钟
		        	</Col>
		        </Row>
		        <Row :gutter="10">
             		<Col  :sm="4" :md="6" :lg="8">
				        <FormItem label="试卷总分：" prop="totalPoints">
				            <Input v-model="formLeft.totalPoints"></Input>
				        </FormItem>
		        	</Col>
		        	<Col :sm="2" :md="2" :lg="2" class="unit">
				     	  	分
		        	</Col>
		        </Row>
		        <Row :gutter="10">
             		<Col :sm="4" :md="6" :lg="8">
				        <FormItem label="及格分数：" prop="passScore">
				            <Input v-model="formLeft.passScore"></Input>
				        </FormItem>
		        	</Col>
		        	<Col :sm="2" :md="2" :lg="2" class="unit">
				     	  	分
		        	</Col>
		        </Row>
		        <FormItem label="试卷类型：" prop="type">
		            <RadioGroup v-model="formLeft.type">
		                <Radio label="1">模拟试卷</Radio>
		                <Radio label="2">正式试卷</Radio>
		                <Radio label="3">公用试卷</Radio> 
		            </RadioGroup>
		        </FormItem>
		        <FormItem label="抽题模式："  class="add-specific">
		        	<Row style="margin-bottom: 10px">
		        		<Col :sm="6" :md="6" :lg="5">
				     	  	step1：选择试题范围
		        		</Col>
		        		<Col :sm="6" :md="4" :lg="2">
				     	  	题型(1845)：
		        		</Col>
		        		<Col :sm="6" :md="6" :lg="4">
				            <Select v-model="formLeft.allSubject.type" placeholder="全部题型" clearable>
				                <Option value="1">单选题</Option>
				                <Option value="2">多选题</Option>
				                <Option value="3">判断题</Option>
				            </Select>
		        		</Col>
		        		<Col :sm="6" :md="6" :lg="4">
				     	  	 <Select v-model="formLeft.allSubject.level" placeholder="题目难度" clearable>
				                <Option value="1">易</Option>
				                <Option value="2">中</Option>
				                <Option value="3">难</Option>
				            </Select>
		        		</Col>
		        	</Row>
		           	 <Row>
		        		<Col :sm="5" :md="5" :lg="5">
				     	  	step2:设置试题数目和分值 
		        		</Col>
		        		<Col :sm="3" :md="3" :lg="2">
				     	  	抽题数量：
		        		</Col>
		        		<Col :sm="2" :md="3" :lg="2">
				           <Input v-model="formLeft.allSubject.num"></Input>
		        		</Col>
		        		<Col :sm="1" :md="1" :lg="1">
				           题
		        		</Col>
		        		<Col :sm="4" :md="3" :lg="2" offset="1">
				     	  	每题分值：
		        		</Col>
		        		<Col :sm="2" :md="3" :lg="2">
				           <Input v-model="formLeft.allSubject.score"></Input>
		        		</Col>
		        		<Col :sm="1" :md="1" :lg="1">
				           分
		        		</Col>
		        		<Col :sm="3" :md="3" :lg="3">
				           <Button type="info" @click='extractingQuestion'>添加</Button>
		        		</Col>
		        	</Row>
		        </FormItem>
		        <div class="extracting-question" v-if="subjectArr.length>0">
		        	<h4>您已添加试题：（试卷总分【{{formLeft.totalPoints}}】分，您已配置【{{allScore}}】分，题目总数：{{questionNum}}）</h4>
		        	<div class="extracting-question-list">
		        		<p v-for='(v,i) in subjectArr'>【{{v.type=='1'?'单选题':(v.type=='2'?'多选题':'判断题')}}--{{v.level=='1'?'易':(v.level=='2'?'中':(v.level=='3'?'难':'综合难度'))}}】，试题{{v.list.length}}道,每题分值{{v.unitScore}},合计{{(v.list.length)*(v.unitScore)}}分。
		        			<Button icon='ios-trash'@click='delItem(i)'></Button> 
		        		</p>
		        	</div>
		        </div>
		        <FormItem label="是否启用：" prop="status">
		            <RadioGroup v-model="formLeft.status">
		                <Radio label="1">启用</Radio>
		                <Radio label="0">禁用</Radio>
		            </RadioGroup>
		        </FormItem>
		         <FormItem>
		            <Button type="primary" @click='addSave("formLeft")'>保存</Button>
		            <Button type="ghost" @click="cancel" style="margin-left: 8px" >取消</Button>
		        </FormItem>
		    </Form>
            	
        </Card>
    </div>
</template>
<script>
import Cookies from 'js-cookie';
export default {
    data() {
        return {
	    	formLeft: {
                name: '',
                duration: '',
                totalPoints: '',
                passScore: '',
                type:'',
                status:'',
                allSubject:{
                	//题目类型
                	type:'',
                	//随机生成数
                	num:'',
                	//难度
                	level:'',
                	//每题分数
                	score:''
                }
           	},
            //抽到之后传到后端的题目
            subjectArr:[],
            //验证
            ruleValidate: {
                name: [
                    { required: true, message: '请输入试卷名称', trigger: 'blur' }
                ],
                duration: [
                    { required: true, message: '请输入考试用的时间', trigger: 'blur' }
                ],
                totalPoints: [
                    { required: true, message: '请输入考试总分', trigger: 'blur' }
                ],
                passScore: [
                    { required: true, message: '请输入考试及格分', trigger: 'blur' }
                ],
                type: [
                    { required: true, message: '请输入试卷类型', trigger: 'change' }
                ],
                status: [
                    { required: true, message: '请选择是否禁用', trigger: 'change' }
                ]
            }
        }
    },
    computed:{
    	//计算配置分数
    	allScore:function(){
    		var num=0;
    		this.subjectArr.forEach(function(v){
    			num=num+v.list.length*v.unitScore
			})
    		return num;
    	},
    	// 题目总数
    	questionNum:function(){
			var num=0;
    		this.subjectArr.forEach(function(subjectArr){
    			num=num+subjectArr.list.length
			})
    		return num;
    	}
    },
    methods: {
    	// 取消
    	cancel:function(){
    		this.$router.push({
    			name:'test-management'
    		})
    	},
    	//抽题
    	extractingQuestion:function(){
    		if(this.formLeft.allSubject.type==''&&this.formLeft.allSubject.num==''&&this.formLeft.allSubject.level==''){
    			this.$Message.error('未添加内容');
    		}else{
    			if(this.formLeft.totalPoints!=""){
	    			if(this.formLeft.allSubject.score==''){
			    		this.$Message.error('未设置每题分值');
			    	}else{
			    		var that=this;
			    		var type1=(this.formLeft.allSubject.type==""||this.formLeft.allSubject.type==undefined)?"":('type='+this.formLeft.allSubject.type);
			    		var level1=(this.formLeft.allSubject.level==""||this.formLeft.allSubject.level==undefined)?"":('&level='+this.formLeft.allSubject.level);
			    		var num1=this.formLeft.allSubject.num==""?"":('&num='+this.formLeft.allSubject.num);
			    		var strData=type1+level1+num1;
			    		if(strData[0]=="&"){
			    			strData=strData.substr(1,strData.length)
			    		}
			    		this.axios({
			    			method:'get',
			    			url:that.$store.state.app.globalUrl+'/admin/Subject/getRandomSubject?'+strData
			    		}).then(function(res){
							if(res.data.msg=='操作成功'){
				    				if(that.allScore<=that.formLeft.totalPoints){
				    					if(that.formLeft.allSubject.type==""||that.formLeft.allSubject.type==undefined){
				    						var subjectObj1={
			    								type:1,
				    							unitScore:that.formLeft.allSubject.score,
				    							level:((that.formLeft.allSubject.level==''||that.formLeft.allSubject.level==undefined)?'':that.formLeft.allSubject.level),
				    							list:[]
				    						};
				    						var subjectObj2={
				    							type:2,
				    							unitScore:that.formLeft.allSubject.score,
				    							level:((that.formLeft.allSubject.level==''||that.formLeft.allSubject.level==undefined)?'':that.formLeft.allSubject.level),
				    							list:[]
				    						};
				    						var subjectObj3={
				    							type:3,
				    							unitScore:that.formLeft.allSubject.score,
				    							level:((that.formLeft.allSubject.level==''||that.formLeft.allSubject.level==undefined)?'':that.formLeft.allSubject.level),
				    							list:[]
				    						};
				    						res.data.data.list.forEach(function(mylist,i){
				    							if(mylist.type=='1'){
				    								subjectObj1.list=subjectObj1.list.concat(mylist);
				    							}
				    							if(mylist.type=='2'){
				    								subjectObj2.list=subjectObj2.list.concat(mylist);
				    							}
				    							if(mylist.type=='3'){
				    								subjectObj3.list=subjectObj3.list.concat(mylist);
				    							}
				    						})
				    						if(subjectObj1.list.length>0){
				    							//为了难度
				    							var mylevel=subjectObj1.list[0].level;
				    							if(subjectObj1.level==''&&subjectObj1.list.length>1){
					    							subjectObj1.list.forEach(function(data1,i){
				    									if(mylevel==data1.level&&subjectObj1.level!='0'){
				    										subjectObj1.level=mylevel
				    									}else{
				    										subjectObj1.level=0
				    									}
					    							})
				    							}else{
				    								subjectObj1.level=mylevel
				    							}
				    							that.subjectArr=that.subjectArr.concat(subjectObj1);
				    						}
			    							if(subjectObj2.list.length>0){
			    								//为了难度
			    								var mylevel=subjectObj2.list[0].level;
				    							if(subjectObj2.level==''&&subjectObj2.list.length>1){
					    							subjectObj2.list.forEach(function(data1,i){
				    									if(mylevel==data1.level&&subjectObj2.level!='0'){
				    										subjectObj2.level=mylevel
				    									}else{
				    										subjectObj2.level=0
				    									}
					    							})
				    							}else{
				    								subjectObj2.level=mylevel
				    							}
			    								that.subjectArr=that.subjectArr.concat(subjectObj2);
			    							}
				    						if(subjectObj3.list.length>0){
				    							//为了难度
				    							var mylevel=subjectObj3.list[0].level;
				    							if(subjectObj3.level==''&&subjectObj3.list.length>1){
					    							subjectObj3.list.forEach(function(data1,i){
				    									if(mylevel==data1.level&&subjectObj3.level!='0'){
				    										subjectObj3.level=mylevel
				    									}else{
				    										subjectObj3.level=0
				    									}
					    							})
				    							}else{
				    								subjectObj3.level=mylevel
				    							}
			    								that.subjectArr=that.subjectArr.concat(subjectObj3);
			    							}
				    					}else{
				    						var subjectObj={
					    					 	type:that.formLeft.allSubject.type,
					    					 	unitScore:that.formLeft.allSubject.score,
					    					 	level:((that.formLeft.allSubject.level==''||that.formLeft.allSubject.level==undefined)?'':that.formLeft.allSubject.level),
					    					 	list:res.data.data.list
					    					};
					    					//为了难度问题
					    					if(subjectObj.level==''){
					    						var levelFlag=res.data.data.list[0].level;
					    						subjectObj.list.forEach(function(mylist){
					    							if(levelFlag==mylist.level&&subjectObj.level!='0'){
					    								subjectObj.level=levelFlag
					    							}else{
					    								subjectObj.level=0
					    							}
					    						})
					    					}
				    						that.subjectArr=that.subjectArr.concat(subjectObj);
				    					}
					    			}else{
					    				that.$Message.error('配置分数大于试卷总分数，请改变总分数或者改变每题分值');
					    			}
						    	
							}else{
								that.$Message.error('题目不够');
							}
			    		})
				    }
		    	}else{
				    this.$Message.error('未设置总分');
				}
    		}
    	},
    	// 删除抽的题
    	delItem:function(i){
    		this.subjectArr.splice(i,1);
    	},
    	//保存
    	addSave:function(name){
    		this.$refs[name].validate((valid) => {
                if (valid) {
                	var that=this;
                	if(this.subjectArr.length>0){
			    		this.axios({
			    			method:'post',
			    			url:that.$store.state.app.globalUrl+'/admin/TestPaper/add',
			    			data:{
			    				name:that.formLeft.name,
			    				duration:that.formLeft.duration,
			    				totalPoints:that.formLeft.totalPoints,
			    				passScore:that.formLeft.passScore,
			    				type:that.formLeft.type,
			    				status:that.formLeft.status,
			    				allSubject:JSON.stringify(that.subjectArr)
			    			}
			    		}).then(function(res){
			    			if(res.data.msg=='操作成功'){
				    			that.$Message.success('添加成功');
					 			that.$router.push({
					 				name:'test-management'	
					 			})
				 			}else{
				     			that.$Message.error('修改失败');
				 			}
			    		})
                	}else{
                		this.$Message.error('试题不能为空!');
                	}
                } else {
                    this.$Message.error('请填写完整!');
                }
            })
    		
    	}
    }
};
</script>
<style scoped>
.add-test .unit{line-height: 33px}
.add-test .ivu-col{margin-right: 10px}
.extracting-question{margin-left: 100px;}
.extracting-question-list{margin-left: 100px;}
/*删除抽题*/
.extracting-question-list .ivu-btn{border:0px;border-radius: none;background:none;padding: 0px;font-size: 30px;margin-left: 20px}
</style>